package com.sgb.kjwl.utils;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.sgb.kjwl.view.widget.ClearEditText;

/**
 * @author: 杨畔
 * @date: 2020/12/29 16:38
 * @Description: 输入完成范围监听
 */
public class RangeFocusChangeListener implements View.OnFocusChangeListener {
    private final double min;
    private final double max;

    public RangeFocusChangeListener(double min, double max) {
        this.min = min;
        this.max = max;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        //兼容带删除按钮输入框
        if (v instanceof ClearEditText) {
            ((ClearEditText) v).onFocusChange(v, hasFocus);
        }
        if (hasFocus) return;
        if (v instanceof EditText) {
            EditText editText = (EditText) v;
            if (TextUtils.isEmpty(editText.getText())) {
                return;
            }
            double number = NumberUtil.convertToDouble(editText.getText().toString());
            if (number > max) {
                editText.setText(NumberUtil.trim0(max));
            } else if (number < min) {
                editText.setText(NumberUtil.trim0(min));
            }
        }
    }
}
